package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.Result;
import com.example.dto.SCommentDTO;
import com.example.entity.SComment;
import com.example.service.SCommentService;
import com.example.service.SOrderService;
import com.example.vo.SCommentVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Api(tags = "评论相关接口", value = "评论相关接口")
@Slf4j
public class SCommentController {
    @Autowired
    SCommentService sCommentService;
    @Autowired
    SOrderService sOrderService;

    @GetMapping
    //id代表spuId，option为0是差评，1是中评，2是好评，3是查所有
    public Result<List<SCommentVO>> getAllCommentBySpuId(@RequestParam Long id,
                                                         @RequestParam(required = false, defaultValue = "3") Integer option) {
        List<SCommentVO> allCommentBySpuId = sCommentService.getAllCommentBySpuId(id, option);
        return Result.success(allCommentBySpuId);
    }

    @PostMapping("/add")
    public Result<?> addComment(@RequestBody SCommentDTO sCommentDTO) {
        log.info("添加评论：{}", sCommentDTO);
        SComment sComment = BeanUtil.copyProperties(sCommentDTO, SComment.class);
        sCommentService.save(sComment);
        sOrderService.updateState(sCommentDTO.getOrderId(), 5);
        return Result.success();
    }
}
